﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Collections;

namespace PickGold.Web.Modules
{
	/// <summary>
	/// 为 ASP.NET 会话状态使用的集合，以管理会话。
	/// </summary>
	public sealed class SessionStateItemCollection : NameObjectCollectionBase, ISessionStateItemCollection, IDictionary
	{
		#region class IDictionaryEnumerator IDictionary.GetEnumerator()

		class DictionaryEnumerator : IDictionaryEnumerator
		{
			public SessionStateItemCollection Session;
			public IEnumerator Enumerator;

			#region IDictionaryEnumerator 成员

			DictionaryEntry IDictionaryEnumerator.Entry
			{
				get
				{
					return new DictionaryEntry(this.Enumerator.Current, this.Session[this.Enumerator.Current + string.Empty]);
				}
			}

			object IDictionaryEnumerator.Key
			{
				get
				{
					return this.Enumerator.Current;
				}
			}

			object IDictionaryEnumerator.Value
			{
				get
				{
					return this.Session[this.Enumerator.Current + string.Empty];
				}
			}

			#endregion

			#region IEnumerator 成员

			object IEnumerator.Current
			{
				get
				{
					return this.Enumerator.Current;
				}
			}

			bool IEnumerator.MoveNext()
			{
				return this.Enumerator.MoveNext();
			}

			void IEnumerator.Reset()
			{
				this.Enumerator.Reset();
			}

			#endregion
		}

		#endregion

		private bool _Dirty = false;

		#region ISessionStateItemCollection 成员

		void ISessionStateItemCollection.Clear()
		{
			base.BaseClear();
			this._Dirty = true;
		}

		bool ISessionStateItemCollection.Dirty
		{
			get
			{
				return this._Dirty;
			}
			set
			{
				this._Dirty = value;
			}
		}

		NameObjectCollectionBase.KeysCollection ISessionStateItemCollection.Keys
		{
			get
			{
				return base.Keys;
			}
		}

		void ISessionStateItemCollection.Remove(string name)
		{
			base.BaseRemove(name);
			this._Dirty = true;
		}

		void ISessionStateItemCollection.RemoveAt(int index)
		{
			base.BaseRemoveAt(index);
			this._Dirty = true;
		}

		object ISessionStateItemCollection.this[int index]
		{
			get
			{
				return base.BaseGet(index);
			}
			set
			{
				base.BaseSet(index, value);
				this._Dirty = true;
			}
		}

		object ISessionStateItemCollection.this[string name]
		{
			get
			{
				return base.BaseGet(name);
			}
			set
			{
				base.BaseSet(name, value);
				this._Dirty = true;
			}
		}

		#endregion

		#region ICollection 成员

		void ICollection.CopyTo(Array array, int index)
		{
			((ICollection)this).CopyTo(array, index);
		}

		int System.Collections.ICollection.Count
		{
			get
			{
				return base.Count;
			}
		}

		bool ICollection.IsSynchronized
		{
			get
			{
				//return true;
				return false;
			}
		}

		object ICollection.SyncRoot
		{
			get
			{
				return this;
				//return null;
			}
		}

		#endregion

		#region IEnumerable 成员

		IEnumerator IEnumerable.GetEnumerator()
		{
			return base.GetEnumerator();
		}

		#endregion

		#region IDictionary 成员

		void IDictionary.Add(object key, object value)
		{
			base.BaseAdd(key + string.Empty, value);
			this._Dirty = true;
		}

		void IDictionary.Clear()
		{
			base.BaseClear();
			this._Dirty = true;
		}

		bool IDictionary.Contains(object key)
		{
			var name = key + string.Empty;
			foreach (string k in base.Keys)
			{
				if (string.Compare(k, name, true) == 0)
					return true;
			}

			return false;
		}

		IDictionaryEnumerator IDictionary.GetEnumerator()
		{
			return new DictionaryEnumerator { Session = this, Enumerator = base.GetEnumerator() };
		}

		bool IDictionary.IsFixedSize
		{
			get
			{
				return false;
			}
		}

		bool IDictionary.IsReadOnly
		{
			get
			{
				return false;
			}
		}

		ICollection IDictionary.Keys
		{
			get
			{
				return base.Keys;
			}
		}

		void IDictionary.Remove(object key)
		{
			base.BaseRemove(key + string.Empty);
			this._Dirty = true;
		}

		ICollection IDictionary.Values
		{
			get
			{
				return base.BaseGetAllValues();
			}
		}

		object IDictionary.this[object key]
		{
			get
			{
				return base.BaseGet(key + string.Empty);
			}
			set
			{
				base.BaseSet(key + string.Empty, value);
				this._Dirty = true;
			}
		}

		#endregion

		internal object this[string name]
		{
			get
			{
				return base.BaseGet(name);
			}
			set
			{
				base.BaseSet(name, value);
				this._Dirty = true;
			}
		}

		internal object Get(string name)
		{
			return base.BaseGet(name);
		}

		internal void Set(string name, object value)
		{
			base.BaseSet(name, value);
			this._Dirty = true;
		}
	}
}
